<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/elementui/index.js"></script>
    <style>
        .demo-table-expand {
            font-size: 0;
        }

        .demo-table-expand label {
            width: 90px;
            color: #99a9bf;
        }

        .demo-table-expand .el-form-item {
            margin-right: 0;
            margin-bottom: 0;
            width: 50%;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="权限名称/关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form ref="dataAddForm" :model="formData"
                                     label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name" style="width:280px"/>
                                        </el-form-item>
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword" style="width:280px"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input v-model="formData.description" style="width:280px"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="second">
                            <template>
                                <el-table
                                        :data="tableData"
                                        style="width: 100%"
                                        stripe
                                        border
                                        row-key="id"
                                        @selection-change="handleSelectionChange"
                                >
                                    <el-table-column
                                            label="选择菜单"
                                            width="80" type="selection">
                                    </el-table-column>
                                    <el-table-column
                                            label="点击展开"
                                            width="180"
                                            type="expand"
                                    >
                                        <template slot-scope="props">
                                            <el-form label-position="left" inline class="demo-table-expand">
                                                <el-form-item label="菜单名称">
                                                    <span>{{ props.row.name }}</span>
                                                </el-form-item>
                                                <el-form-item label="菜单链接">
                                                    <span>{{ props.row.linkUrl }}</span>
                                                </el-form-item>
                                                <el-form-item label="菜单路径">
                                                    <span>{{ props.row.path }}</span>
                                                </el-form-item>
                                                <el-form-item label="权重登记">
                                                    <span>{{ props.row.priority }}</span>
                                                </el-form-item>
                                                <el-form-item label="菜单图标">
                                                    <span>{{ props.row.icon }}</span>
                                                </el-form-item>
                                                <el-form-item label="菜单描述">
                                                    <span>{{ props.row.description }}</span>
                                                </el-form-item>
                                                <el-form-item label="父菜单">
                                                    <span>{{ props.row.parentMenuId }}</span>
                                                </el-form-item>
                                                <el-form-item label="菜单等级">
                                                    <span>{{ props.row.level }}</span>
                                                </el-form-item>
                                            </el-form>
                                        </template>
                                    </el-table-column>
                                    <el-table-column
                                            prop="name"
                                            label="菜单名称"
                                            sortable
                                            width="180">
                                    </el-table-column>
                                    <el-table-column
                                            prop="path"
                                            label="菜单路径"
                                            sortable
                                            width="180">
                                    </el-table-column>
                                    <el-table-column
                                            prop="description"
                                            label="菜单说明">
                                    </el-table-column>
                                </el-table>
                            </template>
                        </el-tab-pane>

                        <el-tab-pane label="权限信息" name="Third">
                            <el-table :data="tableData1"
                                      style="width: 100%"
                                      stripe
                                      border
                                      row-key="id"
                                      @selection-change="handleSelectionChange1"
                            >
                                <el-table-column
                                        label="选择" type="selection">
                                </el-table-column>
                                <el-table-column
                                        prop="name"
                                        label="权限名称"
                                >
                                </el-table-column>
                                <el-table-column
                                        prop="keyword"
                                        label="权限关键字"
                                >
                                </el-table-column>
                            </el-table>
                        </el-tab-pane>
                    </el-tabs>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form
                                    label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name" style="width:280px"/>
                                        </el-form-item>
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword" style="width:280px"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input v-model="formData.description" style="width:280px"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="second">
                            <table style="width: 80%">
                                <thead>
                                <tr >
                                    <th>选择</th>
                                    <th>菜单名称</th>
                                    <th>菜单路径</th>
                                    <th>菜单说明</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="c in tableData">
                                    <td >
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input :id="c.id" v-model="menuIds1" type="checkbox" :value="c.id">
                                    </td>
                                    <td ><label :for="c.id">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{c.name}}</label></td>
                                    <td ><label :for="c.id">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{c.path}}</label></td>
                                    <td ><label :for="c.id">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{c.description}}</label></td>
                                </tr>
                                </tbody>
                            </table>
                        </el-tab-pane>

                        <el-tab-pane label="权限信息" name="Third">
                            <table style="width: 80%">
                                <thead>
                                <tr >
                                    <th>选择</th>
                                    <th>菜单名称</th>
                                    <th>菜单路径</th>
                                    <th>菜单说明</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="c in tableData1">
                                    <td >
                                        &nbsp;<input :id="c.id" v-model="permissionIds1" type="checkbox" :value="c.id">
                                    </td>
                                    <td ><label :for="c.id">{{c.name}}</label></td>
                                    <td ><label :for="c.id">{{c.keyword}}</label></td>
                                </tr>
                                </tbody>
                            </table>
                        </el-tab-pane>
                    </el-tabs>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            multipleSelection: [],
            menuIds: [],//选中的菜单id 回显id和提交数据
            menuIds1: [],//选中的菜单id 回显id和提交数据
            menus1: [],//选中的菜单id 回显id和提交数据
            permissionIds: [],//选中的权限id 回显id和提交数据
            permissionIds1: [],//选中的权限id 回显id和提交数据
            permissions1: [],//选中的权限id 回显id和提交数据
            tableData: [],//用于回显菜单信息
            tableData1: [],//用于回显权限信息
            tableData2: [],//用于回显权限信息
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            value: '',
            roleId:'',
        },
        methods: {
            handleSelectionChange(val) {
                this.menus = [];
                for (const a of val) {
                    let index = a.id - 1;
                    this.menuIds[index] = a.id;
                }
            },
            handleSelectionChange1(val) {
                this.permissions = [];
                for (const b of val) {
                    let index = b.id - 1;
                    this.permissionIds[index] = b.id;
                }
                console.log(this.permissions);
                this.multipleSelection = val;
            },
            //分页查询
            findPage() {
                axios.get("/roleManage/findPage", {params: this.pagination}).then(resp => {
                    //flag查询状态
                    if (resp.data.flag) {
                        //返回的结果类型为result result再次封装查询结果
                        this.dataList = resp.data.data.rows;
                        this.pagination.total = resp.data.data.total;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                })
            },
            handleEdit(){
                axios.post("/roleManage/updateRole" ,this.formData).then(resp => {
                    if (resp.data.flag) {
                        this.$message.success(resp.data.message);
                        this.dialogFormVisible4Edit=false;
                        this.findPage();
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
                axios.get("/roleManage/updateMenuAndRole?menuIds1="+this.menuIds1+"&roleId="+this.roleId).then(resp=>{
                    if(resp.data.flag){
                        this.dialogFormVisible4Edit=false;
                        this.findPage();
                        this.$message.success(resp.data.message);
                    }else{
                        this.$message.error(resp.data.message);
                    }
                });
                axios.get("/roleManage/updatePermissionAndRole?permissionIds1="+this.permissionIds1+"&roleId="+this.roleId).then(resp=>{
                    if(resp.data.flag){
                        this.dialogFormVisible4Edit=false;
                        this.findPage();
                        this.$message.success(resp.data.message);
                    }else{
                        this.$message.error(resp.data.message);
                    }
                })
            },
            handleAdd() {
                this.$refs["dataAddForm"].validate(valid => {
                    if (valid) {
                        //校验通过发送请求
                        axios.post("/roleManage/add?menuIds=" + this.menuIds + "&permissionIds=" + this.permissionIds, this.formData).then(resp => {
                            this.dialogFormVisible = false;
                            if (resp.data.flag) {
                                this.$message.success(resp.data.message);
                            } else {
                                this.$message().error(resp.data.message);
                            }
                        }).catch(error => {
                            this.showMessage(error);
                        }).finally(() => {
                            this.findPage();
                        });
                    } else {
                        this.permissionIds=null;
                        //校验失败
                        this.$message.error("表单有误");
                    }
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {}
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                axios.get("/roleManage/findAllMenu").then(res => {
                    if (res.data.flag) {
                        this.tableData = res.data.data
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
                axios.get("/roleManage/findAllPermission").then(res => {
                    if (res.data.flag) {
                        this.tableData1 = res.data.data
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.roleId=row.id;
                axios.get("/roleManage/findRole?roleId=" + row.id).then(resp => {
                    if (resp.data.flag) {
                        this.formData = resp.data.data;
                        this.dialogFormVisible4Edit = true;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
                //发送请求获取所有菜单信息
                axios.get("/roleManage/findAllMenu").then(resp => {
                    if (!resp.data.flag) {
                        this.$message.error(resp.data.message);
                    }
                    this.tableData = resp.data.data;
                });
                axios.get("/roleManage/findAllPermission").then(res => {
                    if (res.data.flag) {
                        this.tableData1 = res.data.data
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
                axios.get("/roleManage/findMenuByRoleId?roleId=" + row.id).then(resp => {
                    if (resp.data.flag) {
                        this.menuIds1 = resp.data.data;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
                axios.get("/roleManage/findPermissionByRoleId?roleId=" + row.id).then(resp => {
                    if (resp.data.flag) {
                        this.permissionIds1 = resp.data.data;

                    } else {
                        this.$message.error(resp.data.message);
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //当页码发生改变是将发生改变的页码赋值给当前页码
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("是否确定删除此条记录", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    //发送请求
                    axios.delete("/roleManage/deleteRoleById/" + row.id).then(resp => {
                        if (resp.data.flag) {
                            this.$message.success(resp.data.message);
                        } else {
                            this.$message.error(resp.data.message);
                        }
                    }).catch(error => {
                        this.showMessage(error);
                    }).finally(() => {
                        this.findPage();
                    });
                })
            },
            showMessage(r) {//权限不足提示
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
        },
        mounted() {
            this.findPage();
        }
    })
</script>
</html>